package com.dfec.dao.imp;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.dfec.dao.INewsDao;
import com.dfec.model.News;
import com.dfec.model.User;
import com.dfec.utils.DataSourceUtils;
import com.dfec.utils.DruidPlugin;

public class NewsDaoImp implements INewsDao{
//	两种连接池切换
//	QueryRunner qr=new QueryRunner(DataSourceUtils.getDatasSource());
	QueryRunner qr=new QueryRunner(DruidPlugin.getDataSource());
	
	@Override
	public List<News> getNewsListByConditions(News news, int page, int size) {
		// TODO Auto-generated method stub
		String basesql="select id,title,summary,carousel,index_pic,news_type,content_type,tag,comment_times,see_count,click_praise,create_time from d_news where  1=1 ";
		String ordersql=" order by create_time desc limit "+(page-1)*size+" , "+size;
		
		StringBuilder sb=new StringBuilder(basesql);
		List<News> re=null;
		try {
			if(null!=news.getTitle())sb.append(" and title like'%"+news.getTitle()+"%'");
			if(null!=news.getTag())sb.append(" and tag like'%"+news.getTag()+"%'");
			if(null!=news.getContent_type())sb.append(" and content_type like'%"+news.getContent_type()+"%'");
			if(null!=news.getNews_type())sb.append(" and news_type like'%"+news.getNews_type()+"%'");
			if(null!=news.getContent())sb.append(" and content like'%"+news.getContent()+"%'");
			sb.append(ordersql);
			re = qr.query(sb.toString(), new BeanListHandler<News>(News.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
			return re.size()>0?re:new ArrayList<News>();
		}
	@Override
	public boolean saveBatchNews(List<News> newsList) throws SQLException {
		// TODO Auto-generated method stubid
		String sql ="insert into d_news (title,content,index_pic,news_type,content_type,tag,create_time,see_count,click_praise,comment_times,summary,carousel) "
				+ "values(?,?,?,?,?,?,now(),0,0,0,?,?)";
		Object[][] params=new Object[newsList.size()][8];
		for (int i = 0; i < newsList.size(); i++) {
				params[i][0]=newsList.get(i).getTitle();
				params[i][1]=newsList.get(i).getContent();
				params[i][2]=newsList.get(i).getIndex_pic();
				params[i][3]=newsList.get(i).getNews_type();
				params[i][4]=newsList.get(i).getContent_type();
				params[i][5]=newsList.get(i).getTag();
				params[i][6]=newsList.get(i).getSummary();
				params[i][7]=newsList.get(i).getCarousel();
		} 
		return qr.batch(sql, params).length>0;
	}
	@Override
	public News getNewsContent(News newsId) throws SQLException {
		// TODO Auto-generated method stub
		String sql="select content from d_news where  id= "+newsId.getId();
		List<News> rs = qr.query(sql, new BeanListHandler<News>(News.class));
		return rs.get(0);
	}
	@Override
	public boolean deleteBatchNewsbyId(String[] newsIds) throws SQLException {
		String sql ="delete from d_news where id=?";
		Object[][] params=new Object[newsIds.length][1];
		for (int i = 0; i < newsIds.length; i++) {
			params[i][0]=newsIds[i];
		} 
		return qr.batch(sql, params).length>0;
	}
	@Override
	public boolean updatePraiseBy(String newsIds,int addOrdel) throws SQLException {
		// TODO Auto-generated method stub
		  String sql="update d_news set click_praise=click_praise+"+addOrdel+" where id="+newsIds;
	   
		return  qr.update(sql)>0;
	}
	@Override
	public boolean updateSeeTimesBy(String newsIds) throws SQLException {
		// TODO Auto-generated method stub
		String sql="update d_news set see_count=see_count+1 where id="+newsIds;
		   
		return qr.update(sql)>0;
	}
	@Override
	public boolean updateCommentTimesBy(String newsIds,int addOrdel) throws SQLException {
		// TODO Auto-generated method stub
		  String sql="update d_news set comment_times=comment_times+"+addOrdel+" where id="+newsIds;
		   
		return qr.update(sql)>0;
	}
	
	@Override
	public int getSeeTimesOrCommentOrSeeByNewsIds(String clum,String newsIds) throws SQLException {
		// TODO Auto-generated method stub
		Object result = qr.query("select "+clum+" from d_news where id="+newsIds,  new ScalarHandler());
		return result==null?0:(int)result;
	}
	@Override
	public List<News> getNewsListCarouselTop5() throws SQLException {
		// TODO Auto-generated method stub
		String basesql="select id,title,summary,carousel,index_pic,news_type,content_type,tag,comment_times,see_count,click_praise,create_time from d_news where  carousel=1  order by create_time desc limit 5";
		//获取新闻轮播
		List<News> re = qr.query(basesql, new BeanListHandler<News>(News.class));
		return re.size()>0?re:new ArrayList<News>();
	}

}
